// Copyright@ChenChao


#include "Player/AuraPlayerController.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Interaction/EnemyInterface.h"

AAuraPlayerController::AAuraPlayerController()
{
	bReplicates = true;		//可复制
}

void AAuraPlayerController::PlayerTick(float DeltaTime)
{
	Super::PlayerTick(DeltaTime);

	CursorTrace();		//光标追踪
}

void AAuraPlayerController::CursorTrace()
{
	//光标命中结果
	FHitResult CursorHit;
	//获取命中结果
	GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
	
	if (!CursorHit.bBlockingHit) return;	//光标下面没有阻碍物,返回

	LastActor = ThisActor;
	ThisActor = Cast<IEnemyInterface>(CursorHit.GetActor());

	/*
	 *高亮敌人的情况:
	 * 1.LastActor = null  && ThisActor = null
	 *		什么都不做
	 * 2.LastActor = null  && ThisActor = valid
	 *		ThisActor -> HighLightAction()
	 * 3.LastActor = valid  && ThisActor = null
	 *		LastActor -> UnHighLightAction()
	 * 4.LastActor = valid  && ThisActor = valid
	 *		{
	 *			4_1.LastActor = ThisActor
	 *				什么都不做
	 *			4_2.LastActor != ThisActor
	 *				LastActor -> UnHighLightAction()
	 *				ThisActor -> HighLightAction()
	 *		}
	 */
	if (LastActor == nullptr) 
	{
		if (ThisActor == nullptr)
		{
			//1.LastActor = null  && ThisActor = null	什么都不做
		}else    
		{
			//2.LastActor = null  && ThisActor = valid
			ThisActor -> HighLightAction();
		}
	}else
	{
		if (ThisActor == nullptr)
		{
			//3.LastActor = valid  && ThisActor = null
			LastActor -> UnHighLightAction();
		}else
		{
			//4.LastActor = valid  && ThisActor = valid
			if (LastActor == ThisActor)
			{
				//4_1.LastActor = ThisActor
			}else
			{
				//4_2.LastActor != ThisActor
				LastActor -> UnHighLightAction();
				ThisActor -> HighLightAction();
			}
		}
	}
}

void AAuraPlayerController::BeginPlay()
{
	Super::BeginPlay();

	//添加映射上下文
	check(AuraContext);
	TObjectPtr<UEnhancedInputLocalPlayerSubsystem> Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
	check(Subsystem);
	Subsystem -> AddMappingContext(AuraContext,0);

	//显示鼠标光标
	bShowMouseCursor = true;
	DefaultMouseCursor = EMouseCursor::Default;

	//在游戏视口显示鼠标
	FInputModeGameAndUI InputModeData;
	InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
	InputModeData.SetHideCursorDuringCapture(false);
	SetInputMode(InputModeData);
	
}

void AAuraPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	//类型转换
	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
	//绑定移动动作
	EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this,&AAuraPlayerController::Move);
	
}

void AAuraPlayerController::Move(const FInputActionValue& InputActionValue)
{
	//获取输入动作结果:W.S.A.D.
	const FVector2D InputAxisVector = InputActionValue.Get<FVector2D>();
	//获取控制旋转
	const FRotator Rotation = GetControlRotation();
	//只取Yaw的值:x.y
	const FRotator YawRotation(0.f,Rotation.Yaw,0.f);
	//获取向前速度
	const FVector ForwardVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	//获取向右速度
	const FVector RightVector = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
	
	//增加移动输入(将输入动作绑定到控制器方向):
	if (APawn* ControlledPawn = GetPawn<APawn>())	//Pawn存在
	{
		ControlledPawn -> AddMovementInput(ForwardVector,InputAxisVector.Y);
		ControlledPawn -> AddMovementInput(RightVector,InputAxisVector.X);
	}
}

